<?php

namespace App\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;

class MoneyLog extends Model
{
    protected $table = 'money_log';
    protected $guarded = [];

    const SUB_BUY_COURSE = 'SUB_BUY_COURSE';
    const SUB_BUY_VIP = 'SUB_BUY_VIP';
    const ADMIN = 'ADMIN';
    const WITHDRAW = 'WITHDRAW';
    const REFUND = 'REFUND';
    const RED_PACKET = 'RED_PACKET';

    const LIVEROOM_LECTURE_GOODS = 'LIVEROOM_LECTURE_GOODS';
    const LIVEROOM_CHANNEL_GOODS = 'LIVEROOM_CHANNEL_GOODS';
    const LIVEROOM_DOC_GOODS = 'LIVEROOM_DOC_GOODS';
    const LIVEROOM_VIP_GOODS = 'LIVEROOM_VIP_GOODS';

    const TG_LECTURE_GOODS = 'TG_LECTURE_GOODS';
    const TG_CHANNEL_GOODS = 'TG_CHANNEL_GOODS';
    const TG_VIP_GOODS = 'TG_VIP_GOODS';


    const QD_LECTURE_GOODS = 'QD_LECTURE_GOODS';
    const QD_CHANNEL_GOODS = 'QD_CHANNEL_GOODS';
    const QD_VIP_GOODS = 'QD_VIP_GOODS';


    const TYPE = [
        self::SUB_BUY_COURSE => [
            'title' => '好友购买课程奖励',
            'type' => 'tg_sum_money'
        ],
        self::SUB_BUY_VIP => [
            'title' => '好友购买VIP奖励',
            'type' => 'tg_sum_money'
        ],
        self::ADMIN => [
            'title' => '系统调整',
            'type' => 'sum_money'
        ],
        self::WITHDRAW => [
            'title' => '账户余额提现',
            'type' => 'sum_money'
        ],
        self::REFUND => [
            'title' => '提现失败退款',
            'type' => 'sum_money'
        ],
        self::RED_PACKET => [
            'title' => '红包奖励',
            'type' => 'sum_money'
        ],
        self::LIVEROOM_LECTURE_GOODS => [
            'title' => '课程出售',
            'type' => 'liveroom_sum_money'
        ],
        self::LIVEROOM_CHANNEL_GOODS => [
            'title' => '专栏出售',
            'type' => 'liveroom_sum_money'
        ],
        self::LIVEROOM_DOC_GOODS => [
            'title' => '文档出售',
            'type' => 'liveroom_sum_money'
        ],
        self::LIVEROOM_VIP_GOODS => [
            'title' => 'VIP出售',
            'type' => 'liveroom_sum_money'
        ],
        self::TG_LECTURE_GOODS => [
            'title' => '推广课程收益',
            'type' => 'tg_sum_money'
        ],
        self::TG_CHANNEL_GOODS => [
            'title' => '推广专栏收益',
            'type' => 'tg_sum_money'
        ],
        self::TG_VIP_GOODS => [
            'title' => '推广VIP收益',
            'type' => 'tg_sum_money'
        ],
        self::QD_LECTURE_GOODS => [
            'title' => '渠道推广课程收益',
            'type' => 'qd_sum_money'
        ],
        self::QD_CHANNEL_GOODS => [
            'title' => '渠道推广专栏收益',
            'type' => 'qd_sum_money'
        ],
        self::QD_VIP_GOODS => [
            'title' => '渠道推广VIP收益',
            'type' => 'qd_sum_money'
        ]
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }


    public static function addLog($user, $money, $type, $desc = '', $relation = '', $relation_id = 0, $qd_id = 0)
    {

        if (empty(self::TYPE[$type])) {
            return false;
        }

        if ($money < 0 && $money * -1 > $user->money) {
            return false;
        }
        \DB::beginTransaction();
        $log = self::query()->create([
            'user_id' => $user->id,
            'qd_id' => $qd_id,
            'money' => $money,
            'type' => $type,
            'desc' => $desc,
            'ip' => request()->ip(),
            'relation' => $relation,
            'relation_id' => $relation_id,
            'day' => date("Ymd", time())
        ]);
        $user->money = $user->money + $money;//更新余额
        if ($money > 0 && !in_array($type, [self::REFUND])) {//更新收益字段
            $user->sum_money = $user->sum_money + $money;//更新总收益
            switch (self::TYPE[$type]['type']) {
                case 'tg_sum_money':
                    $user->tg_sum_money = $user->tg_sum_money + $money;//更新个人推广总收益
                    break;
                case 'qd_sum_money':
                    $user->qd_sum_money = $user->qd_sum_money + $money;//更新渠道推广总收益
                    break;
                case 'liveroom_sum_money':
                    $user->liveroom_sum_money = $user->liveroom_sum_money + $money;//更新直播间总收益
                    break;
            }

        }
        if ($user->save()) {
            if ($user->money >= 0) {
                \DB::commit();
                return $log;
            } else {
                \DB::rollBack();
                return false;
            }
        } else {
            \DB::rollBack();
            return false;
        }


    }
}
